﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using HA.Data;
using HA.Data.DisplayOptions2;
namespace HA.ModFlow.Controls.Windows.DisplayOptions
{
    /// <summary>
    /// winContourOptionV2.xaml 的交互逻辑
    /// </summary>
    public partial class winContourOptionV2 : Window
    {
        public winContourOptionV2(DOContour contourOption)
        {
            InitializeComponent();

            this._contourCopy = (DOContour)contourOption.Clone();
            this.DataContext = _contourCopy;
            this.contour2 = contourOption;
            this.txtPrecision.LostFocus += new RoutedEventHandler(txtPrecision_LostFocus);

            MenuItem itemCopy = new MenuItem();
            itemCopy.Header = "复制";
            itemCopy.Command = ApplicationCommands.Copy;

            //MenuItem itemPaste2 = new MenuItem();
            //itemPaste2.Header = "粘贴";
            //itemPaste2.InputGestureText = "Ctrl+V";
            //itemPaste2.Click += new RoutedEventHandler(itemPaste2_Click);

            ContextMenu cm = new ContextMenu();
            cm.Items.Add(itemCopy);
            //cm.Items.Add(itemPaste2);          
            this.dtGrid.ContextMenu = cm;
        }

        void txtPrecision_LostFocus(object sender, RoutedEventArgs e)
        {
            ResetDrawLine();
        }

     

        private DOContour contour2;


        DOContour _contourCopy;

        public DOContour ContourCopy
        {
            get { return _contourCopy; }
            set { _contourCopy = value; }
        }


        private void LineEnble()
        {
            if (cboContourType.SelectedIndex == 0)
            {
                this.txtLineWith.IsEnabled = true ;
                this.btnLineColor.IsEnabled = false;
            }
            else if (this.cboContourType.SelectedIndex == 1)
            {
                this.txtLineWith.IsEnabled = false;
                this.btnLineColor.IsEnabled = false;
            }
            else
            {
                this.txtLineWith.IsEnabled = true;
                this.btnLineColor.IsEnabled = true;
            }
        }


        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
          
            CreatIsoplethValueAndColors(this._contourCopy.ContourLines);        

            SolidColorBrush solidColorBrush = new SolidColorBrush(Color.FromArgb(this._contourCopy.LineColor.A, this._contourCopy.LineColor.R, this._contourCopy.LineColor.G, this._contourCopy.LineColor.B));
            this.lineRectColor.Fill = solidColorBrush;

            solidColorBrush = new SolidColorBrush(Color.FromArgb(this._contourCopy.ContourLineProperty.LineTagging.TagColor.A, this._contourCopy.ContourLineProperty.LineTagging.TagColor.R, this._contourCopy.ContourLineProperty.LineTagging.TagColor.G, this._contourCopy.ContourLineProperty.LineTagging.TagColor.B));
            this.tagRectColor.Fill = solidColorBrush;

            //临时取消此功能
            //solidColorBrush = new SolidColorBrush(Color.FromArgb(this._contourCopy.ContourLineProperty.LineScalarBar.TitleColor.A, this._contourCopy.ContourLineProperty.LineScalarBar.TitleColor.R, this._contourCopy.ContourLineProperty.LineScalarBar.TitleColor.G, this._contourCopy.ContourLineProperty.LineScalarBar.TitleColor.B));
            //this.titleRectColor.Fill = solidColorBrush;

            solidColorBrush = new SolidColorBrush(Color.FromArgb(this._contourCopy.ContourLineProperty.LineScalarBar.LabelColor.A, this._contourCopy.ContourLineProperty.LineScalarBar.LabelColor.R, this._contourCopy.ContourLineProperty.LineScalarBar.LabelColor.G, this._contourCopy.ContourLineProperty.LineScalarBar.LabelColor.B));
            this.titleFontRectColor.Fill = solidColorBrush;

            switch (this._contourCopy.ContourType)
            {
                case ContourDisplayType.Linear:
                    this.cboContourType.SelectedIndex = 0;
                    break;
                case ContourDisplayType.ColorFill:
                    this.cboContourType.SelectedIndex = 1;
                    break;
                case ContourDisplayType.LinearAndColorFill:
                    this.cboContourType.SelectedIndex = 2;
                    break;
            }

            //目前屏蔽此功能 
            //switch (this._contourCopy.ColorMode)
            //{
            //    case ContourColorMode.SpecifiedEachColor:
            //        this.cboColorMode.SelectedIndex = 0;
            //        break;
            //    case  ContourColorMode.UseColorRamp:
            //        this.cboColorMode.SelectedIndex = 1;
            //        break;
            //}
            LineEnble();
        }

        private void CreatIsoplethValueAndColors(List<LinearContourColorValue> Isopleth)
        {
         
          
            this.dtGrid.Columns.Clear();
            this.dtGrid.CanUserSortColumns = false;
            this.dtGrid.CanUserAddRows = false;
            this.dtGrid.AutoGenerateColumns = false;
            DataGridTextColumn txt = new DataGridTextColumn();
          

            txt.Header = "值";
            Binding bin = new Binding("Value");
            txt.IsReadOnly = true;
            txt.Binding = bin;
            if (this._contourCopy.IsUseScientificNotation)
            {
              string   str = "{0:E"+this ._contourCopy.DecimalDigitNum.ToString ()+"}";
                txt.Binding.StringFormat = str;
            }
            else
            {
                string str = "{0:f" + this._contourCopy.DecimalDigitNum.ToString() + "}";               
                txt.Binding.StringFormat = str;
            }
            this.dtGrid.Columns.Add(txt);



            DataGridTemplateColumn dc = new DataGridTemplateColumn();
            dc.CellTemplate = this.FindResource("myTemplate") as DataTemplate;
            dc.Header = "颜色";
            this.dtGrid.Columns.Add(dc);

            this.dtGrid.ItemsSource = Isopleth;
            this.dtGrid.Items.Refresh();
        }

     

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            if (this.contour2.ContourLines.Count != this.ContourCopy.ContourLines.Count)
            {
                this.ContourCopy.IsNeedUpdateData  = true;
            }
            else
            {
                for (int i = 0; i < this.ContourCopy.ContourLines.Count; i++)
                {
                    if (this.contour2.ContourLines[i].Value != this.ContourCopy.ContourLines[i].Value || this.contour2.ContourLines[i].Color != this.ContourCopy.ContourLines[i].Color)
                    {
                        this.ContourCopy.IsNeedUpdateData  = true;
                        break;
                    }
                }
            }
            if (this._contourCopy.IsDataChanged())
            {            
                this.DialogResult = true;
                this.Close();
            }
            else
            {
                this.Close();
            }
        }

        private void btnColor_Click(object sender, RoutedEventArgs e)
        {
            if (this.dtGrid.SelectedIndex != -1)
            {
                System.Windows.Forms.ColorDialog dlg = new System.Windows.Forms.ColorDialog();
                dlg.Color = this._contourCopy.ContourLines[this.dtGrid.SelectedIndex].Color;
                if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    this._contourCopy.ContourLines[this.dtGrid.SelectedIndex].Color = dlg.Color;
                    CreatIsoplethValueAndColors(this._contourCopy.ContourLines);
                }
            }
        }

        private void btnCancal_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void dtGrid_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            e.Row.Header = (e.Row.GetIndex() + 1).ToString();
        }

     
        private void btnReset_Click(object sender, RoutedEventArgs e)
        {
            ResetDrawLine();
        }

        private void ResetDrawLine()
        {
            if (this.chkRange.IsChecked.Value)
            {
                if (this._contourCopy.SpRangeMin >= this._contourCopy.SpRangeMax)
                {
                    MessageBox.Show("指定范围的最大值必须大于最小值", "提示", MessageBoxButton.OK);
                    return;
                }
            }
            _contourCopy.BuildContourlines();
            CreatIsoplethValueAndColors(this._contourCopy.ContourLines);
        }


        private void cboContours_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.cboContours.SelectedIndex == 0)
            {
                this.txtContours.Visibility = System.Windows.Visibility.Hidden;
                this.txtCount.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                this.txtContours.Visibility = System.Windows.Visibility.Visible;
                this.txtCount.Visibility = System.Windows.Visibility.Hidden;
            }
        }

        private void txtContours_LostFocus(object sender, RoutedEventArgs e)
        {
            _contourCopy.BuildContourlines();
            CreatIsoplethValueAndColors(this._contourCopy.ContourLines);
        }

        private void btnFont_Click(object sender, RoutedEventArgs e)
        {
            
            System.Windows.Forms.FontDialog dlg = new System.Windows.Forms.FontDialog();

            dlg.ShowColor= true ;
            dlg.ShowEffects = true;
          
            dlg.ShowDialog();
            //Color color = new Color();
        }

        private void btnLineColor_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.ColorDialog dlg = new System.Windows.Forms.ColorDialog();
            dlg.Color = this._contourCopy.LineColor;
            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                SolidColorBrush solidColorBrush = new SolidColorBrush(Color.FromArgb(dlg.Color.A, dlg.Color.R, dlg.Color.G, dlg.Color.B));
                this.lineRectColor.Fill = solidColorBrush;
                this._contourCopy.LineColor = dlg.Color;
            }
        }

        private void btnTagColor_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.ColorDialog dlg = new System.Windows.Forms.ColorDialog();
            dlg.Color = this._contourCopy.ContourLineProperty.LineTagging.TagColor;
            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                SolidColorBrush solidColorBrush = new SolidColorBrush(Color.FromArgb(dlg.Color.A, dlg.Color.R, dlg.Color.G, dlg.Color.B));
                this.tagRectColor.Fill = solidColorBrush;
                this._contourCopy.ContourLineProperty.LineTagging.TagColor= dlg.Color;
            }
        }

        private void btnTitleColor_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.ColorDialog dlg = new System.Windows.Forms.ColorDialog();
            dlg.Color = this._contourCopy.ContourLineProperty.LineScalarBar.TitleColor;
            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                SolidColorBrush solidColorBrush = new SolidColorBrush(Color.FromArgb(dlg.Color.A, dlg.Color.R, dlg.Color.G, dlg.Color.B));

                //临时取消此功能
                //this.titleRectColor.Fill = solidColorBrush;
                //this._contourCopy.ContourLineProperty.LineScalarBar.TitleColor = dlg.Color;
            }
        }

        private void btnTitleFontColor_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.ColorDialog dlg = new System.Windows.Forms.ColorDialog();
            dlg.Color = this._contourCopy.ContourLineProperty.LineScalarBar.LabelColor;
            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                SolidColorBrush solidColorBrush = new SolidColorBrush(Color.FromArgb(dlg.Color.A, dlg.Color.R, dlg.Color.G, dlg.Color.B));
                this.titleFontRectColor.Fill = solidColorBrush;
                this._contourCopy.ContourLineProperty.LineScalarBar.LabelColor = dlg.Color;
            }
        }

        private void cboContourType_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            switch (this.cboContourType.SelectedIndex)
            {
                case 0:
                    this._contourCopy.ContourType = ContourDisplayType.Linear;
                    break;
                case 1:
                    this._contourCopy.ContourType = ContourDisplayType.ColorFill;
                    break;
                case 2:
                    this._contourCopy.ContourType = ContourDisplayType.LinearAndColorFill;
                    break;
            }
            LineEnble();
        }

        private void cboColorMode_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //目前屏蔽此功能
            //switch (this .cboColorMode.SelectedIndex )
            //{
            //    case 0:
            //        this._contourCopy.ColorMode=ContourColorMode.SpecifiedEachColor;
            //        break;
            //    case 1:
            //        this._contourCopy.ColorMode=ContourColorMode.UseColorRamp;
            //        break;
            //}
        }

        private void chkPrecision_Click(object sender, RoutedEventArgs e)
        {

            ResetDrawLine();

        }

    

       

     

       
    }

    public class ConvertToColor : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            System.Drawing.Color color=( System.Drawing.Color)value ;
            Color clr=Color.FromArgb(color.A, color.R, color.G, color.B);
          
            return  new  System.Windows.Media.SolidColorBrush( clr);
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    public class ConvertToContourInterval : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            int index = 0;
            ContourInterval type = (ContourInterval)value;
            switch (type)
            {
                case ContourInterval.NumberOfContours:
                    index = 0;
                    break;
                case ContourInterval.SpecifiedInterval:
                    index = 1;
                    break;
                //case ContourInterval.SpecifiedValues:
                //    index = 2;
                //    break;
            }
            return index;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            int index = (int)value;
            ContourInterval type = ContourInterval.NumberOfContours;
            switch (index)
            {
                case 0:
                    type = ContourInterval.NumberOfContours;
                    break;
                case 1:
                    type = ContourInterval.SpecifiedInterval;
                    break;
                //case 2:
                //    type = ContourInterval.SpecifiedValues;
                //    break;
            }
            return type;
        }
    }




}
